package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    SetmealService setmealService;

    /**
     * 新增套餐数据
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setMealCache",key = "#setmealDTO.categoryId")
    public Result addMeal(@RequestBody SetmealDTO setmealDTO) {
        setmealService.addMeal(setmealDTO);
        return Result.success();
    }
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true) //清理全部
    public Result deleteMeal(String ids){
        setmealService.deleteMeal(ids);
        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> selectMeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询数据: {}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 根据ID查询套餐
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> selectById(@PathVariable Long id){
        log.info("根据ID查询数据: {}",id);
        SetmealVO setmealVO =  setmealService.selectById(id);
        return Result.success(setmealVO);
    }
    /**
     * 套餐起售、停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售")
    @CacheEvict(cacheNames = "setMealCache",allEntries = true)
    public Result updateStatus(@PathVariable Integer status, Long id){
        setmealService.updateStatus(status,id);
        return Result.success();
    }
    @PutMapping
    @ApiOperation("修改套餐")
    public Result updateSetMeal(SetmealDTO setmealDTO){
        setmealService.updateSetMeal(setmealDTO);
        return Result.success();
    }
}
